package com.zhaolin.contorller;



import com.google.common.base.Strings;
import com.zhaolin.entity.Admin;
import com.zhaolin.service.impl.AdminServiceImpl;
import com.zhaolin.utils.MD5Utils;
import com.zhaolin.utils.MapControl;
import com.zhaolin.utils.MapParameter;
import com.zhaolin.utils.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {

    @Autowired
    private AdminServiceImpl adminService;


    @GetMapping("/login")
    public String login(){
        return "page/login";
    }

    @PostMapping("/login")
    @ResponseBody
    public  Map<String, Object>  longin(@RequestBody Map<String,Object>map, HttpServletRequest request){
        //接受前端mapper的账号
        String account = map.get("account")+"";
        //接受前端mapper的密码
        String password = map.get("password")+"";
        //判断两个字符串是否为空
        if (Strings.isNullOrEmpty(account)||Strings.isNullOrEmpty(password)){
            return MapControl.getInstance().error("用户名密码不能为空").getMap();
        }
            Admin login = adminService.login(account, MD5Utils.getMD5(password));
            if (login !=null){
                SessionUtils.setAdmin(request,login);
                return MapControl.getInstance().success().getMap();
            }else{
                return MapControl.getInstance().error("用户名或密码错误").getMap();
            }

    }
}
